package net.xyt.report.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class ReportDataSource {
    /**
     * @category 处理固定的参数生成报表，不循环。如Map或Entity
     * @param jasperReport
     * @param params
     * @return
     */
	public static JasperPrint dealDataParam(JasperReport jasperReport,Map<String,Object> params) {
		JasperPrint jasperPrint=null;
		try {
			jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return jasperPrint;
	}
	
	/**
	 * @category 处理循环参数生成报表，循环。一切List
	 * @param jasperReport
	 * @param list
	 * @return
	 */
	public static JasperPrint dealDataList(JasperReport jasperReport,List<? extends Object> list) {
		JasperPrint jasperPrint=null;
		try {
			JRDataSource dataSource = new JRBeanCollectionDataSource(list);
			jasperPrint = JasperFillManager.fillReport(jasperReport, null,dataSource);
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jasperPrint;
	}
}
